using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Globalization;
using System.Windows.Forms;

namespace Diversity.Robotics.Navigation.GridSlamApp
{
    public partial class LogFileImport : Form
    {
        private Form1 _parent;
        private RobotFromLogFile _robotFromLogFile;
        public LogFileImport(Form1 parent)
        {
            _parent = parent;
            InitializeComponent();
        }

        private void buttonLogFileSelectFile_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DialogResult result = openFileDialogLogFile.ShowDialog();

            if (result == DialogResult.OK && openFileDialogLogFile.CheckFileExists)
            {
                string displayFileName = openFileDialogLogFile.FileName;
                int lastSlash = displayFileName.LastIndexOf('\\') + 1;
                if (lastSlash > 0 && lastSlash < displayFileName.Length)
                    labelLogFileName.Text = displayFileName.Substring(lastSlash,displayFileName.Length-lastSlash);
                labelLogFileName.Refresh();
                labelLogFileStatus.Text = "Parsing...";
                labelLogFileStatus.Refresh();
                _robotFromLogFile = new RobotFromLogFile(new Pose2D(0, 0, 0), 10, 10, new Map(100, 100, 0.6, 10), openFileDialogLogFile.FileName, _parent.BeamModel, _parent.ConeModel, double.Parse(textBoxLogFileLaserFOV.Text, CultureInfo.InvariantCulture), double.Parse(textBoxLogFileLaserX.Text, CultureInfo.InvariantCulture), double.Parse(textBoxLogFileLaserY.Text, CultureInfo.InvariantCulture), double.Parse(textBoxMapScale.Text, CultureInfo.InvariantCulture));
                labelLogFileStatus.Text = _robotFromLogFile.status;
                if (_robotFromLogFile.success == true)
                {
                    labelLogFileMaxX.Text = _robotFromLogFile.maxX.ToString();
                    labelLogFileMaxY.Text = _robotFromLogFile.maxY.ToString();
                    labelLogFileMinX.Text = _robotFromLogFile.minX.ToString();
                    labelLogFileMinY.Text = _robotFromLogFile.minY.ToString();
                    labelLogFileNumBeams.Text = _robotFromLogFile.numBeams.ToString();
                    labelLogFileNumLaser.Text = _robotFromLogFile.numScans.ToString();
                    labelLogFileNumOdometry.Text = _robotFromLogFile.numOdometry.ToString();
                    labelLogFileMaxRange.Text = _robotFromLogFile.maxRange.ToString();
                    labelLogFileResultantMap.Text =
                        string.Format("{0} x {1}", _robotFromLogFile.GlobalMap.Width, _robotFromLogFile.GlobalMap.Height);
                    
                    buttonLogFileImport.Enabled = true;
                }
            }
        }

        private void buttonLogFileCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonLogFileImport_Click(object sender, EventArgs e)
        {
            _parent.LogFileRobot = _robotFromLogFile;
            this.Close();
        }

      
    }
}